<?php
class UserAction extends LoginedAction {
    public function index(){
		$this->display("person");
    }
    
    public function tpl() {
    	$name = $this->_get("name");
    	if (method_exists($this,$name)) {
    		$this->$name();
    	} else {
    	   	$this->display("person-".$name);
    	}
    }
    
    public function uploadAvatar() {
    	import('ORG.Net.UploadFile');
    	$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = 1024000 ;// 设置附件上传大小
		$upload->thumb = true;
		$upload->thumbMaxWidth = "200";
		$upload->thumbMaxHeight = "200";
		$upload->allowExts  = array('jpg','png');// 设置附件上传类型
		$upload->savePath =  './data/avatars/';// 设置附件上传目录
		if(!$upload->upload()) {
			echo json_encode(array("error"=>1,"msg"=>L($upload->getErrorMsg())));
		}else{
			if ($this->_user["userface"]) {
				@unlink('./data/avatars/'.$this->_user["userface"]);
				@unlink('./data/avatars/thumb_'.$this->_user["userface"]);
			}
			$info = $upload->getUploadFileInfo();
			M("User")->data(array("userface"=>$info[0]['savename']))->where(array("id"=>$this->_user["id"]))->save();
			echo json_encode(array("error"=>0,"msg"=>$info[0]['savename']));
		}	
    }
    
    
    public function project() {
    	import('ORG.Util.Page');
    	$type = $this->_get("type");
    	$tpl = "view";
    	$query = array();
    	if ($type == "bid") {
    		$tpl = "view-bid";
    		$query = array("userid"=>$this->_user["id"],"status"=>1);
    	} else if ($type == "close") {
    		$tpl = "view-close";
    		$query = array("userid"=>$this->_user["id"],"status"=>4);
    	} else {
    		$type = "work";
    		$query = array("userid"=>$this->_user["id"],"status"=>3);
    	}
    	$_count = M("project")->where($query)->count();
    	$_page = new Page($_count,15);
    	$_page->url = U('/space/'.$tpl."-__PAGE__");
    	$_list = M("project")->where($query)
    			->limit($_page->firstRow.','.$_page->listRows)
    			->select();
    	$this->assign("_page",$_page->show());
    	$this->assign("_type",$type);
    	$this->assign("_list",$_list);
    	$this->display("person-".$tpl);
    }
    
    public function bidding() {
    	import('ORG.Util.Page');
    	$type = $this->_get("type");
    	$tpl = "bid";
    	$query = array();
    	if ($type == "view") {
    		$tpl = "bid-view";
    		$query = array("uid"=>$this->_user["id"],"vso_probid.status"=>3);
    	} else if ($type == "close") {
    		$tpl = "bid-close";
    		$query = array("uid"=>$this->_user["id"],"vso_probid.status"=>4);
    	} else {
    		$type = "bid";
    		$query = array("uid"=>$this->_user["id"],"vso_probid.status"=>1);
    	}
    	$_count = M("probid")->where($query)->count();
    	$_page = new Page($_count,15);
    	$_page->url = U('/space/'.$tpl."-__PAGE__");
    	$_list = M("probid")->where($query)
    			->join("__PROJECT__ on __PROJECT__.id = __PROBID__.pid")
    			->limit($_page->firstRow.','.$_page->listRows)
    			->select();
    	//echo M("probid")->getLastSql();
    	$this->assign("_page",$_page->show());
    	$this->assign("_type",$type);
    	$this->assign("_list",$_list);
    	$this->display("person-".$tpl);
    }
    
    public function profile() {
    	if ($this->_post("_submit")) {
    		$data = array();
    		if ($this->_post("country")) {
    			$data["country"] = M("country")->getFieldByEn($this->_post("country"),"code");
    		}
    		$data["usertype"] = intval($this->_post("usertype"));
    		$data["contact"] = serialize(array("tel"=>$this->_post("tel"),"im"=>$this->_post("im")));
    		$data["userskill"] = $this->_post("skill");
    		$data["intro"] = $this->_post("intro");
    		M("User")->data($data)->where(array("id"=>$this->_user["id"]))->save();
    		redirect(U('/space/profile'));
    		exit();
    	}
    	$ukey = urlencode(authcode(serialize(cookie('vusertoken')),"ENCODE",C("SITE_KEY"),900));
    	$this->assign("ukver",$ukey);
    	$this->display("person-profile");
    	
    }
    
    public function publish() {
    	if ($this->_post("_submit")) {
    		$bid_time = array(0,3,5,10,15,20);
    		$_project = M('project');
    		$_project->create($_POST);
    		$_project->status = 1;
    		$_project->userid = $this->_user["id"];
    		$_project->pubtime = time();
    		$_project->endtime = time() + $bid_time[intval($this->_post("last"))]*24*60*60;
    		$_project->add();
    		$this->jumpUrl = U("/space/view");
    		$this->success(L("PERSON_PUB_PROJECT_INFO"));
    	} else {
    		$this->display("person-publish");
    	}
    }
    
    public function casenew() {
    	if ($this->_post("_submit")) {
    		$_case = M('userdemo');
    		$_case->create($_POST);
    		$_case->userid = $this->_user["id"];
    		$_case->add();
    		redirect(U("/space/case"));
    	} else {
    		$this->display("person-case-new");
    	}
    	
    }
    
    public function caseaction() {
    	$id = intval($this->_get("id"));
    	$action = $this->_get("action");
    	$_case = M("userdemo")->where(array("id"=>$id,"userid"=>$this->_user["id"]))->find();
    	if ($_case) {
    		if ($action == "edit") {
    			//var_dump($_case);
    			if ($this->_post("_submit")) {
		    		$_case = M('userdemo');
		    		$_case->create($_POST);
		    		$_case->id = $id;
		    		$_case->userid = $this->_user["id"];
		    		$_case->save();
		    		//echo $_case->getLastSql();
		    		redirect(U("/space/case"));
		    	} else {
		    		$this->assign("case",$_case);
		    		$this->display("person-case-new");
		    	}
    		} else {
    			M('userdemo')->where("id=".$id)->delete();
    			redirect(U("/space/case"));
    		}
    	} else {
    		$this->error("BAD REQUEST");
    	}
    }
    
    public function usercase() {
    	import('ORG.Util.Page');
    	$count = M("userdemo")->where(array("userid"=>$this->_user["id"]))->count();
    	$Page = new Page($count,15);
    	$data = M("userdemo")->where(array("userid"=>$this->_user["id"]))
    			->limit($Page->firstRow.','.$Page->listRows)
    			->select();
    	$this->assign("_list",$data);
    	$this->display("person-case");
    }
    
    public function favorite() {
    	$favtype = $this->_get('type');
    	import('ORG.Util.Page');
    	if ($this->_get("action") == "cancel") {
    		$id = intval($this->_get("id"));
    		if ($favtype == "people") {
    			M("userfollow")->where(array("userid"=>$this->_user["id"],"followuid"=>$id))->delete();
    		} else {
    			M("userfav")->where(array("vso_userfav.userid"=>$this->_user["id"],"favproid"=>$id))->delete();
    		}
    		$this->success(L("FOLLOW HAVE ALREADY CANCEL"));
    		exit();
    	}
    	if ($favtype == "people") {
    		$count = M("userfollow")->where(array("userid"=>$this->_user["id"]))->count();
    		$Page = new Page($count,15);
    		$data = M("userfollow")->where(array("userid"=>$this->_user["id"]))
    				->join("__USER__ on __USER__.id = __USERFOLLOW__.followuid")
    				->limit($Page->firstRow.','.$Page->listRows)
    				->select();
    		$this->assign("_list",$data);
    		$this->assign("_page",$Page->show());
    		$this->display("person-follow-people");
    	} else { //if ($favtype == "project") { 
    		$count = M("userfav")->where(array("userid"=>$this->_user["id"]))->count();
    		$Page = new Page($count,15);
    		$data = M("userfav")->where(array("vso_userfav.userid"=>$this->_user["id"]))
    				->join("__PROJECT__ on __PROJECT__.id = __USERFAV__.favproid")
    				->limit($Page->firstRow.','.$Page->listRows)
    				->select();
    		$this->assign("_list",$data);
    		$this->assign("_page",$Page->show());
    		$this->display("person-follow");
    	} 
    	
    }
    
    public function verify() {
    	$status = $this->_user['isverify'];
    	if ($status == 4) {
    		$this->assign("verify",unserialize($this->_user['verifyinfo']));
    		$this->display("person-cer-already");
    	} else if ($status == 3) {
    		$this->display("person-cer-wait");
    	} else {
    		$_step = intval($this->_get("step"));
	    	if ($_step < 1) {
	    		if ($this->_post("_submit")) {
	    			$varr = array("realname","id","im","about");
	    			foreach($varr as $v) {
	    				$vdata[$v] = $this->_post($v);
	    			}/*
	    			import('ORG.Net.UploadFile');
			    	$upload = new UploadFile();// 实例化上传类
					$upload->maxSize  = 1024000 ;// 设置附件上传大小
					$upload->allowExts  = array('jpg','png');// 设置附件上传类型
					$upload->savePath =  './data/avatars/';// 设置附件上传目录
					if(!$upload->upload()) {
						echo json_encode(array("error"=>1,"msg"=>L($upload->getErrorMsg())));
					}else{
						if ($this->_user["userface"]) {
							@unlink('./data/avatars/'.$this->_user["userface"]);
							@unlink('./data/avatars/thumb_'.$this->_user["userface"]);
						}
						$info = $upload->getUploadFileInfo();
						M("User")->data(array("userface"=>$info[0]['savename']))->where(array("id"=>$this->_user["id"]))->save();
						echo json_encode(array("error"=>0,"msg"=>$info[0]['savename']));
					}*/
	    			M("user")->data(array("verifyinfo"=>serialize($vdata)))->where(array("id"=>$this->_user['id']))->save();
	    			redirect(U("/space/cer")."?step=1");
	    			exit();
	    		}
	    		$this->display("person-cer");
	    	} else if ($_step == 1){
	    		if ($this->_post("_submit")) {
	    			redirect(U("/space/cer")."?step=2");
	    			exit();
	    		}
	    		$this->display("person-cer-tel");
	    	} else if ($_step == 2) {
	    		if ($this->_post("_submit")) {
	    			M("user")->data(array("isverify"=>3))->where(array("id"=>$this->_user['id']))->save();
	    			redirect(U("/space/cer"));
	    			exit();
	    		}
	    		$this->display("person-cer-cer");
	    	}
    	}
    }
    
    public function security() {
    	if ($this->_post("_submit")) {
    		$user = M("User");
    		$nowpass  = $this->_post("oldp",'md5');
    		$password = $this->_post("newp");
    		$repass   = $this->_post("newpa");
    		$data = array();
    		if (md5($nowpass.$this->_user['salt']) != $this->_user['password']) {
    			$tips = alertTips(L("原密码不正确"));
    		} else {
	    		if ($password) {
		    		if ($password == $repass) {
		    			$data["salt"] = substr(md5(time()),5,4);
		    			$data["password"] = md5(md5($password).$data["salt"]);
		    			$tips = alertTips(L("修改成功！您修改了密码，可能需要重新登录"));
		    			$user->data($data)->where(array("id"=>$this->_user["id"]))->save();
		    		} else {
		    			$tips = alertTips(L("两次输入的密码不一致"));
		    		}
	    		} else {
	    			$tips = alertTips(L("请输入新密码"));
	    		}
	    		
	    	}
    	}
    	$this->assign("tips",$tips);
    	$this->assign("data",$this->_user);
    	$this->display("person-password");
    }
    
    public function publish_game()
    {
        if (IS_POST) {
                import('ORG.Net.UploadFile');
                $upload = new UploadFile();// 实例化上传类
                $upload->maxSize  = 1024000 ;// 设置附件上传大小
                $upload->allowExts  = array('swf','jpg','png');// 设置附件上传类型
                $upload->savePath =  './data/attach/';// 设置附件上传目录
                if(!$upload->upload()) {
                        $this->error(L($upload->getErrorMsg()));
                }else{
                    $info = $upload->getUploadFileInfo();
                    //分类id
                    $cat_id1 = $this->_post("cat_id1");
                    $cat_id2 = $this->_post("cat_id2");
                    $cat_id = $cat_id2?$cat_id1.','.$cat_id2:$cat_id1;
                    $data = array(
                        "name" =>$this->_post("name"),
                        "cat_id" =>$cat_id,
                        "desc" => $this->_post("desc"),
                        "img" => $info[0]['savename'],
                        "status" => 1,
                        "flash" => $info[1]['savename'],
                        "add_time"=> time(),
                    );
                    if(!D('Game')->create($data))
                    {
                        $this->error(L(D('Game')->getError()));
                    }
                    D('Game')->add();
                }
    		$this->jumpUrl = U("User/game_list");
    		$this->success(L("PERSON_PUB_PROJECT_INFO"));
    	} else {
                $pcat_list = D('gamecat')->where('pid=0 and is_show=1 and status=1')->select();
                $this->assign('pcat_list', $pcat_list);
    		$this->display("person-game-publish");
    	}
    }
    
    public function ajax_game_sub_cat()
    {
        $cat_id = $this->_get("cat_id");
        $cat_list = D('gamecat')->where('pid='.$cat_id)->select();
        $this->assign('cat_list',$cat_list);
        $content = $this->fetch('Public:_select');
        die($content);
    }
    
    public function game_list()
    {
        import('ORG.Util.Page');
//    	$type = $this->_get("type");
////    	$tpl = "view";
////    	$query = array();
////    	if ($type == "bid") {
////    		$tpl = "view-bid";
////    		$query = array("userid"=>$this->_user["id"],"status"=>1);
////    	} else if ($type == "close") {
////    		$tpl = "view-close";
////    		$query = array("userid"=>$this->_user["id"],"status"=>4);
////    	} else {
////    		$type = "work";
////    		$query = array("userid"=>$this->_user["id"],"status"=>3);
////    	}
        $where = 'status=1 and user_id='.$this->_user["id"];
    	$count = M("game")->where($where)->count();
    	$page = new Page($count,15);
    	//$page->url = U('/space/'.$tpl."-__PAGE__");
    	$goods_list = M("game")->where($where)
    			->limit($page->firstRow.','.$page->listRows)
    			->select();
    	$this->assign("page",$page->show());
    	$this->assign("type",$type);
    	$this->assign("goods_list",$goods_list);
    	$this->display("person-game-list");
    }
    
    public function publish_goods()
    {
        if (IS_POST) {
                import('ORG.Net.UploadFile');
                $upload = new UploadFile();// 实例化上传类
                $upload->maxSize  = 1024000 ;// 设置附件上传大小
                $upload->allowExts  = array('zip','rar','doc','7z','pdf','jpg','png');// 设置附件上传类型
                $upload->savePath =  './data/attach/';// 设置附件上传目录
                if(!$upload->upload()) {
                        $this->error(L($upload->getErrorMsg()));
                }else{
                    $info = $upload->getUploadFileInfo();
                    $cat_id = $this->_post("cat_id1").$this->_post("cat_id2")?','.$this->_post("cat_id"):'';
                    $data = array(
                        "name" =>$this->_post("name"),
                        "cat_id" =>$cat_id,
                        "desc" => $this->_post("desc"),
                        "img" => $info[0]['savename'],
                        "price" => intval($this->_post("price")),
                        "status" => 1,
                        "src" => $info[1]['savename'],
                        "add_time"=> time(),
                    );
                    if(!D('Goods')->create($data))
                    {
                        $this->error(L(D('Goods')->getError()));
                    }
                    D('Goods')->add();
                }
    		$this->jumpUrl = U("User/goods_list");
    		$this->success(L("PERSON_PUB_PROJECT_INFO"));
    	} else {
                $pcat_list = D('goodscat')->where('pid=0 and is_show=1 and status=1')->select();
                $this->assign('pcat_list', $pcat_list);
    		$this->display("person-goods-publish");
    	}
    }
    
    public function ajax_goods_sub_cat()
    {
        $cat_id = $this->_get("cat_id");
        $cat_list = D('goodscat')->where('pid='.$cat_id)->select();
        $this->assign('cat_list',$cat_list);
        $content = $this->fetch('Public:_select');
        die($content);
    }
    
    public function goods_list()
    {
        import('ORG.Util.Page');
//    	$type = $this->_get("type");
////    	$tpl = "view";
////    	$query = array();
////    	if ($type == "bid") {
////    		$tpl = "view-bid";
////    		$query = array("userid"=>$this->_user["id"],"status"=>1);
////    	} else if ($type == "close") {
////    		$tpl = "view-close";
////    		$query = array("userid"=>$this->_user["id"],"status"=>4);
////    	} else {
////    		$type = "work";
////    		$query = array("userid"=>$this->_user["id"],"status"=>3);
////    	}
        $where = 'status=1 and user_id='.$this->_user["id"];
    	$count = M("goods")->where($where)->count();
    	$page = new Page($count,15);
    	//$page->url = U('/space/'.$tpl."-__PAGE__");
    	$goods_list = M("goods")->where($where)
    			->limit($page->firstRow.','.$page->listRows)
    			->select();
    	$this->assign("page",$page->show());
    	$this->assign("type",$type);
    	$this->assign("goods_list",$goods_list);
    	$this->display("person-goods-list");
    }
    
    public function goods_del()
    {
        $id = $this->_get('id');
        $data = array(
            'id' => $id,
            'status' => 0,
        );
        D('Goods')->save($data);
        $this->jumpUrl = U("User/goods_list");
        $this->success(L("PERSON_PUB_PROJECT_INFO"));
    }
    
    protected function _initialize(){
		parent::_initialize();
		$this->assign("nav_menu","space");
	}
}